package com.logisticsrepair;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSON;
import com.google.zxing.BarcodeFormat;
import com.logisticsrepair.config.ThreadPoolTaskConfig;
import com.logisticsrepair.customEntity.UserRolePermissionEntity;
import com.logisticsrepair.entity.*;
import com.logisticsrepair.enums.OrderProcessEnum;
import com.logisticsrepair.enums.RepairStatusEnum;
import com.logisticsrepair.mapper.ApplyMaterialMapper;
import com.logisticsrepair.mapper.UserRoleMapper;
import com.logisticsrepair.service.ApplyMaterialService;
import com.logisticsrepair.service.MaterialLibraryService;
import com.logisticsrepair.service.RepairOrdersService;
import com.logisticsrepair.service.WorkerService;
import com.logisticsrepair.util.RandomPersonalInfo;
import com.logisticsrepair.util.RedisUtil;
import com.logisticsrepair.util.mail.MailService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import redis.clients.jedis.Jedis;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * SpringBootTest在启动的时候不会启动服务器，所以WebSocket自然会报错，
 * 这个时候需要添加选项webEnvironment，以便提供一个测试的web环境
 */
@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class LogisticsrepairApplicationTests {

    @Autowired
    private RepairOrdersService repairOrdersService;

    String openid = "oGFtu6Y6YOKauWPMHZV36LXIQy9M";
    String openid1 = "oGFtu6YjZHuqYMXauIqQKRDOnq2Q";
    String[] openidArr = {openid, openid1};

    int[] typeMark = {1, 2, 3, 4, 5};
    String[] phone = {"14920085545", "15011432966", "15172095163", "19161153627", "15379985941", "17099551526", "13843189796", "19879552336", "13095308020", "16567316148", "13097813629", "15316487832", "17773568828", "14783304504", "15618878255", "18535481811", "15014992990", "13918279935", "13666649023",
            "17044738724", "19921454115", "13343856585", "15708809472", "15102379777", "18691688681", "19980259940", "18617517715", "15087597578", "18750283628", "15602114541", "18443015153", "15206239922", "18847815361", "15377596757", "15211282205", "15272281161", "13588735618", "13817918763", "15862523324",
            "18625398582", "13623001667", "14571132969", "17535624539", "15318012956", "19852952076", "18005677329", "13916949073", "15006267707", "15500402327", "15339403831", "13115147176", "18052131971", "17077781797", "15738667776", "18761516010", "18880274753", "17342607007", "18099998473", "18474733309",
            "15182529604", "17098973631", "14920162646", "17197535248", "15082915691", "15942323947", "13920447976", "13037007914", "18322771195", "14795602409", "18708632898", "13296613597", "13964301496", "18496848768", "15880654970", "17870677381", "13566091263", "19885643061", "13792338877", "15528493970",
            "17517599217", "18512228594", "17053452632", "13919141994", "13688719717", "14778464408", "13437385894", "18103212219", "15738065015", "14588471320", "18198574096", "16634894802", "17194735526", "13055024506", "15693456832", "17052666559", "13686887498", "18190506991", "18582961393", "13805922719"};

    int[] yaer = {2018, 2019, 2020, 2021, 2022};
    @Autowired
    ThreadPoolTaskConfig threadPoolTaskConfig;

    @Autowired
    WorkerService workerService;

    @Autowired
    TemplateEngine templateEngine;

    @Autowired
    MailService mailService;

    @Autowired
    ApplyMaterialService applyMaterialService;

    Jedis redis = RedisUtil.getRedis();

    @Autowired
    UserRoleMapper userRoleMapper;

    @Test
    public void LogisticsrepairApplicationTests_90(){
        UserRolePermissionEntity queryUserType = userRoleMapper.queryUserRoleByUserId(openid);
        System.out.println(queryUserType);
    }

    @Test
    public void LogisticsrepairApplicationTests_84() {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i + 1);
        }

        int size = list.size();
        for (int i = 0; i < size; i++) {
            if (list.get(i) == 6) {
                list.remove(i);
                i--;
                size--;
            }
        }
        System.out.println(list);
    }

    @Autowired
    MaterialLibraryService materialLibraryService;

    @Test
    public void LogisticsrepairApplicationTests_107(){
        List<ApplyMaterialEntity> entityList = applyMaterialService.queryUserWaitChargeOrder(openid);
        entityList.forEach(System.out::println);
    }

    @Test
    public void LogisticsrepairApplicationTests_113(){
        System.out.println(userRoleMapper.queryUserRoleByUserId(openid1));
    }

    @Test
    public void LogisticsrepairApplicationTests_102() {
        List<MaterialLibraryEntity> list = materialLibraryService.list();
        int size = list.size();
        for (int i = 0; i < size; i++) {
            if (list.get(i).getMaterialAmount() <= 0) {
                list.remove(i);
                i--;
                size--;
            }
        }
        list.forEach(System.out::println);
    }


    @Test
    public void LogisticsrepairApplicationTests_85() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("workerOpenid", "oGFtu6Y6YOKauWPMHZV36LXIQy9M");
        List<RepairOrdersEntity> entityList = repairOrdersService.queryWorkerOrder(map);
        entityList.forEach(System.out::println);
    }

    public static String RandomDateTime(int[] year) {
        DateTime dateTime = DateUtil.date();
        dateTime.setField(DateField.YEAR, year[RandomUtil.randomInt(0, year.length)]);
        dateTime.setField(DateField.MONTH, RandomUtil.randomInt(1, 13));
        if (dateTime.getField(DateField.MONTH) != 2) {
            dateTime.setField(DateField.DAY_OF_MONTH, RandomUtil.randomInt(1, 31));
        } else {
            dateTime.setField(DateField.DAY_OF_MONTH, RandomUtil.randomInt(1, 29));
        }
        dateTime.setField(DateField.HOUR_OF_DAY, RandomUtil.randomInt(0, 25));
        dateTime.setField(DateField.MINUTE, RandomUtil.randomInt(0, 60));
        dateTime.setField(DateField.SECOND, RandomUtil.randomInt(0, 60));
        return DateUtil.format(dateTime, DatePattern.NORM_DATETIME_PATTERN);
    }


    //@Transactional(rollbackFor = Exception.class)
    @Test
    public void LogisticsrepairApplicationTests_72() {
        /**
         * {"typeMark":3,"repairAddress":"3423","repairContent":"厕所喷屎","repairPhone":"15281056531"}
         * ==>  Preparing: INSERT INTO repair_orders ( order_id, user_openid, type_mark, repair_address, repair_content, repair_phone, repair_date, status_code ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )
         * ==> Parameters: 20220425100237(String), oGFtu6Y6YOKauWPMHZV36LXIQy9M(String), 3(Integer), 3423(String), 厕所喷屎(String), 15281056531(String), 2022-04-25 10:02:37.874(Timestamp), 1(Integer)
         */

        ArrayList<RepairOrdersEntity> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            RepairOrdersEntity orders = new RepairOrdersEntity();
            //orders.setOrderId(DateUtil.format(date, DatePattern.PURE_DATETIME_PATTERN) + RandomUtil.randomNumbers(4));
            orders.setUserOpenid(openid1);
            orders.setTypeMark(typeMark[RandomUtil.randomInt(0, 5)]);
            orders.setRepairAddress(RandomPersonalInfo.getRoad());
            orders.setRepairContent("房顶漏水");
            orders.setRepairPhone(phone[RandomUtil.randomInt(0, 100)]);
            orders.setRepairDate(RandomDateTime(this.yaer));
            orders.setStatusCode(RepairStatusEnum.HAVE_FINISHED);
            list.add(orders);
        }
        repairOrdersService.saveBatch(list);

        List<RepairOrdersEntity> list1 = repairOrdersService.list();
        for (RepairOrdersEntity entity : list1) {
            OrderProcessEntity orderProcess = new OrderProcessEntity();
            orderProcess.setOrderId(entity.getOrderId());
            orderProcess.setContactConfirmation("success");
        }
    }


    @Test
    public void LogisticsrepairApplicationTests_112() {
        int[] yaer = {2018, 2019, 2020, 2021, 2022};

        for (int i = 0; i < 20; i++) {

        }
    }

    @Test
    public void test() {
        System.out.println(DateUtil.date().toDateStr());
        System.out.println(DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + RandomUtil.randomNumbers(4));
    }

}
